package vip.mall.modular.goods.controller;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import vip.mall.common.result.CommonResult;
import vip.mall.modular.goods.entity.Goods;
import vip.mall.modular.goods.params.AddGoods;
import vip.mall.modular.goods.params.GoodsPageParams;
import vip.mall.modular.goods.service.GoodsService;

import java.util.List;

/**
 * @author ASUS
 * @Date 2025/2/10  10:35
 */

@RestController
@RequestMapping("/goods")
@Tag(name = "商品业务")
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    /**
     * 添加商品
     */
    @PostMapping("/add")
    @Operation(summary = "添加商品信息")
   public CommonResult addGoods(@RequestBody AddGoods addGoods){
        if(ObjectUtil.isEmpty(addGoods)){
            return CommonResult.error("添加商品失败");
        }
        boolean result = goodsService.add(addGoods);
        return result ? CommonResult.success() : CommonResult.error();
    }
    @GetMapping("/list")
    @Operation(summary = "获取所有商品")
    public CommonResult list(){
        return CommonResult.success(goodsService.list());
    }
/**
     * 删除商品
     */
    @GetMapping("/delete")
    @Operation(summary = "删除商品")
    public CommonResult remove(@RequestParam("id") String id){
        boolean result = goodsService.removeById(id);
        return result ? CommonResult.success() : CommonResult.error();

    }
 /**
     * 修改商品信息
     */
    @PostMapping("/update")
    @Operation(summary = "修改商品信息")
    public CommonResult update(@RequestBody Goods goods){
        boolean byId = goodsService.updateById(goods);
        return byId?CommonResult.success():CommonResult.error();
    }
/**
     * 模糊查询商品信息
     */
    @PostMapping("/findGoodsInfo")
    @Operation(summary = "模糊查询商品信息")
    public CommonResult findGoods(String GoodsInfo){
        List<Goods> goodsInfo = goodsService.findGoodsInfo(GoodsInfo);
        return CommonResult.success(goodsInfo);
    }

    /**
     * 分页查询商品数据
     */

    @PostMapping("/page")
    @Operation(summary = "分页查询商品数据")
    public CommonResult pageGoods(@RequestBody GoodsPageParams goodsPageParams){
        return CommonResult.success(goodsService.pageList(goodsPageParams));
    }

    /**
     *  根据类型id查询商品
     */
    @GetMapping("/show")
    @Operation(summary = "根据类型id查询商品")
    public CommonResult showGoods(@RequestParam ("typeId") String typeId){
        return CommonResult.success(goodsService.showGoods(typeId));
    }
}
